home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15174 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointer to Functions and Calls thereof??
  5. Date: 17 Apr 1996 07:02:30 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4l2tlmINN822@keats.ugrad.cs.ubc.ca>
  8. References: <Dq01Ft.Dqn@latcs1.lat.oz.au>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <Dq01Ft.Dqn@latcs1.lat.oz.au>,
  12. Eric Woelkerling <woelkerl@lion.cs.latrobe.edu.au> wrote:
  13. >    Hi!    Can anybody fill me in as to how to get a pointer to a function
  14. >and then use this to do the call??  I have a situation where a particular function
  15.  
  16. Wow. An actual _on topic_ question for comp.lang.c. Amazing!
  17.  
  18. >will be selected from a set of functions and used throughout my entire code,
  19. >but the function selected will depend on the runtime selection by thge user..
  20. >
  21. >so far.. I have got as far as..
  22. >
  23. >
  24. >    void    func1(void){naughty bits}
  25. >    void    func2(void){naughty bits}
  26. >
  27. >    void    main(void)
  28. >    {    void    *a[1];
  29. >
  30. >        a[1]=func1;
  31. >        a[2]=func2;
  32. >
  33. >        (*a[1])();
  34. >    }
  35. >
  36. >    Is there something obvious I am missing here??  I am on a PC and using 
  37. >borland c++ compiler... 
  38.  
  39. You are missing the fact that the object a has been declared as a one-membered
  40. array of type (void *). It is not a function pointer. In fact, it can't even be
  41. used to store function pointers, since it is illegal in C to covert between
  42. void * and function pointers.
  43.  
  44. Secondly, when you declare an array  TYPE a[1], you may only access elements
  45. a[0] and a[1]. Accessing element a[2] is not allowed. The C Standard calls it
  46. undefined behavior.
  47.  
  48. To store function pointers, you must use an object that has a compatible type.
  49. In your case, the two functions have a new-style empty parameter list and
  50. return void. To declare a compatible function pointer, you would use a
  51. declaration like:
  52.  
  53.         void (* funptr)(void);
  54.  
  55. The first parentheses override the precedence of the (void) parameter list.
  56. Without them, you would be declaring a function returning void *, not a
  57. pointer to a function returning void.
  58.  
  59. You then assign to funptr in a straightforward manner:
  60.  
  61.         funptr = func1;
  62.  
  63. Function pointers are treated specially by C. There is no need to use a
  64. dereference operator when you want to call through them. To call the function
  65. pointed at by funptr, you just do:
  66.  
  67.         funptr();
  68.  
  69. That's it.
  70.  
  71. By the way, if you want an array of two functions, you can declare this:
  72.  
  73.     void (* funarray[3])(void);
  74.  
  75. >    If you can help,  please mail me directly     Thanks!
  76.  
  77. Okay, but please go to the newsgroup. It may be worthwhile.
  78. -- 
  79. I'm not really a jerk, but I play one on Usenet.
  80.